﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using C1.Phone.FlexGrid;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using C1.Phone;
using System.Windows.Media;

namespace jbgswin
{
    internal enum quyu
    {
        海曙区 = 3,
        江北区 = 4,
        江东区 = 5,
        鄞州区 = 6
    }

    public partial class peoplelist : PhoneApplicationPage
    {
        private ManualResetEvent me = new ManualResetEvent(true);
        public peoplelist()
        {
            InitializeComponent();
            BuildLocalizedApplicationBar();
            //  this = App.myditu.OrderBy(h=>h.nowid).Take(20).ToList();
            this.mygrid.ItemsSource = App.myditu.OrderBy(h => h.nowid).Take(20).ToList();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < this.mygrid.Rows.Count; i++)
            {
                this.mygrid.Rows[i].Selected = true;
            }
            //  me.WaitOne();
            //for (int i = 0; i < this.mylist.Items.Count; i++)
            //{
            //    C1ListBoxItem lb = this.mylist.ItemContainerGenerator.ContainerFromIndex(i) as C1ListBoxItem;
            //    //  DataTemplate da = lb.FindName("DataTemplate1") as DataTemplate;
            //    //    CheckBox cka = FindFirstElementInVisualTree2<CheckBox>(lb) as CheckBox;
            //    // StackPanel targetItem = FindFirstElementInVisualTree<StackPanel>(lb) as StackPanel;
            //    // StackPanel targetItem = da.FindName("StackPanel") as StackPanel;
            //    //      CheckBox cka = targetItem.FindName("ck") as CheckBox;
            //    //  StackPanel stack = (StackPanel)lb.FindName("sa");
            //    //        CheckBox cka = stack.FindName("ck") as CheckBox;
            //    //  cka.IsChecked = true;
            //    SearchVisualTreet(lb);

            //}
            //   me.Reset();
        }
        private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
        {
            var count = VisualTreeHelper.GetChildrenCount(parentElement);
            var b = count;
            if (count == 0)
                return null;

            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(parentElement, i);

                if (child != null && child is T)
                {
                    return (T)child;
                }
                else
                {
                    var result = FindFirstElementInVisualTree<T>(child);
                    if (result != null)
                        return result;

                }
            }
            return null;
        }
        private T FindFirstElementInVisualTree2<T>(DependencyObject parentElement) where T : DependencyObject
        {
            //var count = VisualTreeHelper.GetChildrenCount(parentElement);
            //var b = count;
            //if (count == 0)
            //    return null;

            //for (int i = 0; i < count; i++)
            //{
            //    var child = VisualTreeHelper.GetChild(parentElement, i);

            //    if (child != null && child is T)
            //    {
            //        return (T)child;
            //    }
            //    else
            //    {
            //        var result = FindFirstElementInVisualTree<T>(child);
            //        if (result != null)
            //            return result;

            //    }
            //}
            //return null;
            while (parentElement != null)
            {
                if (parentElement is T)
                    return parentElement as T;
                parentElement = VisualTreeHelper.GetChild(parentElement, 0);
            }
            return null;
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            App.toimport.Clear();
           /* for (int i = 0; i < this.mygrid.Rows.Selected.Count; i++)
            {
               /* if ( this.mygrid.Rows[i].Selected == true)
                {#1#
                    App.toimport.Add(
                        App.myditu.ToList()
                            .Where(h => h.nowid.ToString() == this.mygrid.Rows[i][1].ToString())
                            .FirstOrDefault());
               // }
               
            }*/
            foreach (Row rw in this.mygrid.Rows.Selected)
            {
                App.toimport.Add(
                       App.myditu.ToList()
                           .Where(h => h.nowid.ToString() == rw[1].ToString())
                           .FirstOrDefault());
            }
            //List<dituwz> ga = new List<dituwz>();
            //for (int i = 0; i < this.mylist.Items.Count; i++)
            //{
            //    C1ListBoxItem lb = this.mylist.ItemContainerGenerator.ContainerFromIndex(i) as C1ListBoxItem;
            //    //  DataTemplate da = lb.FindName("DataTemplate1") as DataTemplate;
            //    /*  CheckBox cka = FindFirstElementInVisualTree<CheckBox>(lb) as CheckBox;
            //      //  StackPanel stack = (StackPanel)lb.FindName("sa");
            //      //        CheckBox cka = stack.FindName("ck") as CheckBox;
            //     if (cka.IsChecked == true)
            //     {
            //         App.toimport.Add(App.myditu.ToList().Where(h=>h.Id.ToString()==cka.Tag.ToString()).FirstOrDefault());
            //     }*/
            //    searchtonavc(lb, ga);

            //}
            //App.toimport.AddRange(ga.Distinct().ToList());
            ////  MessageBox.Show(ga.Distinct().Count().ToString());
            NavigationService.Navigate(new Uri("/chakan.xaml", UriKind.Relative));
            ////  MessageBox.Show(App.toimport.Count.ToString());
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            App.toimport.Clear();
           // this.mygrid.
//             for (int i = 0; i < this.mygrid.Rows.Count; i++)
//             {
//                 if (this.mygrid.Rows[i].Selected == true)
//                 {
                    NavigationService.Navigate(new Uri("/addpeople.xaml?id=" + this.mygrid.Rows[mygrid.SelectedIndex][1].ToString(), UriKind.Relative));
                    /*App.toimport.Add(
                        App.myditu.ToList()
                            .Where(h => h.nowid.ToString() == this.mygrid.Rows[i][1].ToString())
                            .FirstOrDefault());*/
              //  }

         //   }
            
            //for (int i = 0; i < this.mylist.Items.Count; i++)
            //{
            //    C1ListBoxItem lb = this.mylist.ItemContainerGenerator.ContainerFromIndex(i) as C1ListBoxItem;
            //    //  DataTemplate da = lb.FindName("DataTemplate1") as DataTemplate;
            //    /* CheckBox cka = FindFirstElementInVisualTree<CheckBox>(lb) as CheckBox;
            //     int a = 0;
            //     //  StackPanel stack = (StackPanel)lb.FindName("sa");
            //     //        CheckBox cka = stack.FindName("ck") as CheckBox;
            //     if (cka.IsChecked == true)
            //     {
            //       //  App.toimport.Add(App.myditu.ToList().Where(h => h.Id.ToString() == cka.Tag.ToString()).FirstOrDefault());
            //         NavigationService.Navigate(new Uri("/addpeople.xaml?id=" + cka.Tag.ToString(), UriKind.Relative));
            //     }*/
            //    searchtonav(lb);


            //}
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < this.mygrid.Rows.Count; i++)
            {
                this.mygrid.Rows[i].Selected = false;
            }
            
            //for (int i = 0; i < this.mylist.Items.Count; i++)
            //{
            //    C1ListBoxItem lb = this.mylist.ItemContainerGenerator.ContainerFromIndex(i) as C1ListBoxItem;
            //    /*StackPanel sp = FindFirstElementInVisualTree<StackPanel>(lb) as StackPanel;
            //    //  DataTemplate da = lb.FindName("DataTemplate1") as DataTemplate;
            //    CheckBox cka = sp.FindName("ck") as CheckBox;
            //    int c = 0;
            //    //  StackPanel stack = (StackPanel)lb.FindName("sa");
            //    //        CheckBox cka = stack.FindName("ck") as CheckBox;
            //    cka.IsChecked = false;*/
            //    SearchVisualTree(lb);
            //}
        }
        private void searchtonavc(DependencyObject targetElement, List<dituwz> g)
        {
            var count = VisualTreeHelper.GetChildrenCount(targetElement);
            if (count == 0)
                return;
            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(targetElement, i);
                if (child is StackPanel)
                {
                    StackPanel targetItem = (StackPanel)child;

                    CheckBox cka = targetItem.FindName("ck") as CheckBox;
                    //  int c = 0;
                    //  StackPanel stack = (StackPanel)lb.FindName("sa");
                    //        CheckBox cka = stack.FindName("ck") as CheckBox;
                    if (cka.IsChecked == true)
                    {
                        //  NavigationService.Navigate(new Uri("/addpeople.xaml?id=" + cka.Tag.ToString(), UriKind.Relative));
                        g.Add(App.myditu.ToList().Where(h => h.nowid.ToString() == cka.Tag.ToString()).FirstOrDefault());
                    }

                    return;
                }
                else
                {
                    if (child != null)
                    {
                        searchtonavc(child, g);
                    }
                }
            }
        }
        private void searchtonav(DependencyObject targetElement)
        {
            var count = VisualTreeHelper.GetChildrenCount(targetElement);
            if (count == 0)
                return;
            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(targetElement, i);
                if (child is StackPanel)
                {
                    StackPanel targetItem = (StackPanel)child;

                    CheckBox cka = targetItem.FindName("ck") as CheckBox;
                    //   int c = 0;
                    //  StackPanel stack = (StackPanel)lb.FindName("sa");
                    //        CheckBox cka = stack.FindName("ck") as CheckBox;
                    if (cka.IsChecked == true)
                    {
                        NavigationService.Navigate(new Uri("/addpeople.xaml?id=" + cka.Tag.ToString(), UriKind.Relative));
                    }

                    return;
                }
                else
                {
                    searchtonav(child);
                }
            }
        }
        private void SearchVisualTree(DependencyObject targetElement)
        {
            var count = VisualTreeHelper.GetChildrenCount(targetElement);
            if (count == 0)
                return;
            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(targetElement, i);
                if (child is StackPanel)
                {
                    StackPanel targetItem = (StackPanel)child;

                    CheckBox cka = targetItem.FindName("ck") as CheckBox;
                    // int c = 0;
                    //  StackPanel stack = (StackPanel)lb.FindName("sa");
                    //        CheckBox cka = stack.FindName("ck") as CheckBox;
                    cka.IsChecked = false;
                    return;
                }
                else
                {
                    SearchVisualTree(child);
                }
            }
        }
        private void SearchVisualTreet(DependencyObject targetElement)
        {
            // CheckBox a = new CheckBox();
            var count = VisualTreeHelper.GetChildrenCount(targetElement);
            if (count == 0)
                return;
            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(targetElement, i);
                if (child is CheckBox)
                {
                    CheckBox cka = child as CheckBox;
                    ///   if (a != cka)
                    // {
                    // StackPanel targetItem = (StackPanel) child;
                    // a = cka;
                    //  CheckBox cka = targetItem.FindName("ck") as CheckBox;
                   // int c = 0;
                    //  StackPanel stack = (StackPanel)lb.FindName("sa");
                    //        CheckBox cka = stack.FindName("ck") as CheckBox;
                    if (cka.IsChecked == false)
                        cka.IsChecked = true;
                    return;
                    //   }

                }
                else
                {
                    if (child != null)
                    {

                        SearchVisualTreet(child);
                    }
                }
            }
        }
        private void SearchVisualTreetk(DependencyObject targetElement, CheckBox yp)
        {
            var count = VisualTreeHelper.GetChildrenCount(targetElement);
            if (count == 0)
                return;
            for (int i = 0; i < count; i++)
            {
                var child = VisualTreeHelper.GetChild(targetElement, i);
                if (child is StackPanel)
                {
                    StackPanel targetItem = (StackPanel)child;

                    CheckBox cka = targetItem.FindName("ck") as CheckBox;
                    // int c = 0;
                    //  StackPanel stack = (StackPanel)lb.FindName("sa");
                    //  CheckBox cka = targetItem.FindName("ck") as CheckBox;
                    if (cka == yp)
                    {
                        if (yp.IsChecked == true)
                        {
                            cka.IsChecked = false;
                            return;
                        }
                        //   cka.IsChecked = yp.IsChecked;
                        /*
                        if (cka.IsChecked == false)
                        {
                            cka.IsChecked = true;
                            return;
                        }
                        else
                        {
                            cka.IsChecked = false;
                        }*/
                    }
                }
                else
                {
                    if (child != null)
                    {

                        SearchVisualTreetk(child, yp);
                    }
                }
            }
        }
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            ListPickerItem ltm = this.pck.SelectedItem as ListPickerItem;
            int yu = (int)Enum.Parse(typeof(quyu), ltm.Content.ToString());
            //   MessageBox.Show(   yu.ToString());
            List<dituwz> oj = getbyid(yu.ToString());
            this.mygrid.ItemsSource = oj.Distinct().OrderBy(h => h.nowid).ToList();
        }

        private void BuildLocalizedApplicationBar()
        {
            // 将页面的 ApplicationBar 设置为 ApplicationBar 的新实例。
            ApplicationBar = new ApplicationBar();

            // 创建新按钮并将文本值设置为 AppResources 中的本地化字符串。
            ApplicationBarIconButton appBarButton =
                new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.back.rest.png", UriKind.Relative));
            // appBarButton.Text = AppResources.AppBarButtonText;
            appBarButton.Text = "返回主页";
            ApplicationBar.Buttons.Add(appBarButton);
            appBarButton.Click += appBarButton_Click;
        }

        private void appBarButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        public List<dituwz> getbyid(string pid)
        {
            List<dituwz> ld = new List<dituwz>();
            List<dituwz> lds = App.myditu.Where(h => h.nowid.ToString() == pid).ToList();
            ld.AddRange(lds);
            foreach (dituwz z in lds)
            {
                GetChild(ld, int.Parse(z.nowid.ToString()));
            }
            return ld;
        }

        private void GetChild(List<dituwz> ld, int p)
        {
            if (App.myditu.Where(h => h.pid.ToString() == p.ToString()).Count() == 0)
            {
                return;
            }
            else
            {
                List<dituwz> d = App.myditu.Where(h => h.pid.ToString() == p.ToString()).ToList();
                ld.AddRange(d);
                //  throw new NotImplementedException();
                foreach (dituwz z in d)
                {
                    GetChild(ld, int.Parse(z.nowid.ToString()));
                }
            }
        }



        private void ck_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void ck_Click_1(object sender, RoutedEventArgs e)
        {
            /* CheckBox pkb = sender as CheckBox;
             for (int i = 0; i < this.mylist.Items.Count; i++)
             {
                 C1ListBoxItem lb = this.mylist.ItemContainerGenerator.ContainerFromIndex(i) as C1ListBoxItem;
                 //  DataTemplate da = lb.FindName("DataTemplate1") as DataTemplate;
                 // CheckBox cka = FindFirstElementInVisualTree<CheckBox>(lb) as CheckBox;
                 // StackPanel targetItem = FindFirstElementInVisualTree<StackPanel>(lb) as StackPanel;
                 /* StackPanel targetItem = lb.FindName("StackPanel") as StackPanel;
                  CheckBox cka = targetItem.FindName("ck") as CheckBox;
          //  StackPanel stack = (StackPanel)lb.FindName("sa");
         //        CheckBox cka = stack.FindName("ck") as CheckBox;
           cka.IsChecked = true;#1#
                 SearchVisualTreetk(lb,pkb);
             }*/
            /*  if (pkb.IsChecked == true)
              {
                  pkb.IsChecked = false;
              }
              else
              {
                  pkb.IsChecked = true;
              }*/
        }

        private void ck_DoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            (sender as CheckBox).IsChecked = false;
        }

        private void mygrid_Click(object sender, RoutedEventArgs e)
        {
           
        }
    }
}